package com.putao.album.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.InputFilter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.putao.album.R;
import com.putao.album.util.StringHelper;
import com.putao.album.util.ViewHelper;

/**
 * Created by jidongdong on 15/4/29.
 */
public class ImportDialogBuilder extends DialogBuilder {

    private DialogButtonOnClickListener mDialogButtonOnClickListener;
    private EditText etx_tag;
    private int mInputlengthLimit = 8;
    private String inputText;

    protected ImportDialogBuilder(Context context) {
        super(context);
    }

    public ImportDialogBuilder setDialogButtonOnClickListener(DialogButtonOnClickListener listener) {
        mDialogButtonOnClickListener = listener;
        return this;
    }

    public interface DialogButtonOnClickListener {
        void positiveOnClick(Dialog dialog, String importText);

        void negativeOnClick(Dialog dialog, String importText);
    }

    /**
     * 设置输入内容字符长度限制
     *
     * @param length
     */
    public ImportDialogBuilder setInputLimit(int length) {
        if (length < 0) {
            throw new IllegalArgumentException("length is must >=0");
        }
        mInputlengthLimit = length;
        return this;
    }

    public ImportDialogBuilder setInputText(String inputText) {
        this.inputText=inputText;
        return this;
    }


    @Override
    protected int getContentViewId() {
        return R.layout.dialog_pt_import_layout;
    }

    @Override
    protected void doInitSubView(View view) {
        etx_tag = ViewHelper.queryViewById(view, R.id.etx_tag);

        if (etx_tag != null) {
            etx_tag.setFilters(new InputFilter[]{new InputFilter.LengthFilter(mInputlengthLimit)});
        }
        if(!StringHelper.isEmpty(inputText)){
            etx_tag.setText(inputText);
        }
        TextView tv_message = ViewHelper.queryViewById(view, R.id.tv_message);
        if (!StringHelper.isEmpty(getMessage())) {
            tv_message.setText(getMessage());
        }
        Button positiveButton = ViewHelper.queryViewById(view, R.id.positiveButton);
        Button negativeButton = ViewHelper.queryViewById(view, R.id.negativeButton);
        if (!StringHelper.isEmpty(getPositiveButtonText())) {
            positiveButton.setText(positiveButtonText);
            positiveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mDialogButtonOnClickListener != null && etx_tag.getText() != null) {
                        mDialogButtonOnClickListener.positiveOnClick(getDialog(), etx_tag.getText().toString());
                    }
                }
            });

        } else {
            positiveButton.setVisibility(View.GONE);
        }

        if (!StringHelper.isEmpty(getNegativeButtonText())) {
            negativeButton.setText(negativeButtonText);
            negativeButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if (mDialogButtonOnClickListener != null && etx_tag.getText() != null) {
                        mDialogButtonOnClickListener.negativeOnClick(getDialog(), etx_tag.getText().toString());
                    }
                }
            });
        } else {
            negativeButton.setVisibility(View.GONE);
        }
    }

    @Override
    protected void doSetupData() {

    }
}
